use std::process::Command;
use tauri::AppHandle;

#[tauri::command]
pub fn run_shell(_: AppHandle, command: String) -> Result<String, String> {
  let output = if cfg!(target_os = "windows") {
    Command::new("cmd").args(&["/C", &command]).output()
  } else {
    Command::new("sh").args(&["-c", &command]).output()
  };

  match output {
    Ok(output) => {
      if output.status.success() {
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
      } else {
        Err(String::from_utf8_lossy(&output.stderr).to_string())
      }
    }
    Err(e) => Err(e.to_string()),
  }
}
